package com.geccocrawler.gecco.downloader;

import com.geccocrawler.gecco.annotation.GeccoClass;
import com.google.common.collect.Maps;
import org.reflections.Reflections;

import java.util.Map;
import java.util.Set;

public class DownloaderAOPFactory {

    private Map<String, BeforeDownload> beforeDownloads;

    private Map<String, AfterDownload> afterDownloads;

    public DownloaderAOPFactory(Reflections reflections) {
        this.beforeDownloads = Maps.newHashMap();
        this.afterDownloads = Maps.newHashMap();
        Set<Class<?>> classes = reflections.getTypesAnnotatedWith(GeccoClass.class);
        for (Class<?> aopClass : classes) {
            GeccoClass geccoClass = aopClass.getAnnotation(GeccoClass.class);
            try {
                Class<?>[] geccoClasses = geccoClass.value();
                for (Class<?> c : geccoClasses) {
                    String name = c.getName();
                    Object o = aopClass.newInstance();
                    if (o instanceof BeforeDownload) {
                        beforeDownloads.put(name, (BeforeDownload) o);
                    } else if (o instanceof AfterDownload) {
                        afterDownloads.put(name, (AfterDownload) o);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public BeforeDownload getBefore(String spiderName) {
        return beforeDownloads.get(spiderName);
    }

    public AfterDownload getAfter(String spiderName) {
        return afterDownloads.get(spiderName);
    }

}
